﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace JasLib.Interop.Win32
{
    /// <summary>
    /// Kernel Transaction Manager
    /// </summary>
    public static partial class KTMW32
    {
        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CommitComplete", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CommitComplete(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CommitEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CommitEnlistment(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CommitTransaction", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CommitTransaction([In] HANDLE TransactionHandle);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CommitTransactionAsync", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CommitTransactionAsync([In] HANDLE TransactionHandle);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CreateEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE CreateEnlistment(
            [In] ref SECURITY_ATTRIBUTES lpEnlistmentrAttributes,
            [In] HANDLE ResourceManagerHandle,
            [In] HANDLE TransactionHandle,
            [In] NOTIFICATION_MASK NotificationMask,
            [In] CreateEnlistmentOptions CreateOptions,
            [In] IntPtr EnlistmentKey);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CreateResourceManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE CreateResourceManager(
            [In] ref SECURITY_ATTRIBUTES lpResourceManagerAttributes,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid ResourceManagerID,
            [In] CreateResourceManagerOptions CreateOptions,
            [In] HANDLE TmHandle,
            [In, MarshalAs(UnmanagedType.LPWStr)] string Description);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CreateTransaction", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE CreateTransaction(
            [In] ref SECURITY_ATTRIBUTES lpTransactionAttributes,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid UOW,
            [In] CreateTransactionOptions CreateOptions,
            [In] uint IsolationLevel,
            [In] uint IsolationFlags,
            [In] uint Timeout,
            [In, MarshalAs(UnmanagedType.LPWStr)] string Description);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "CreateTransactionManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE CreateTransactionManager(
            [In] ref SECURITY_ATTRIBUTES lpTransactionAttributes,
            [In, MarshalAs(UnmanagedType.LPWStr)] string LogFileName,
            [In] CreateTransactionManagerOptions CreateOptions,
            [In] uint CommitStrength = 0);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetCurrentClockTransactionManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetCurrentClockTransactionManager(
            [In] HANDLE EnlistmentHandle,
            [Out] out LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetEnlistmentId", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetEnlistmentId(
            [In] HANDLE EnlistmentHandle,
            [Out] out Guid EnlistmentId);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetEnlistmentRecoveryInformation", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetEnlistmentRecoveryInformation(
            [In] HANDLE EnlistmentHandle,
            [In] uint BufferSize,
            [In] IntPtr Buffer,
            [Out] out uint BufferUsed);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetNotificationResourceManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetNotificationResourceManager(
            [In] HANDLE ResourceManagerHandle,
            [Out] out TRANSACTION_NOTIFICATION TransactionNotification,
            [In] uint NotificationLength,
            [In] uint dwMilliseconds,
            [Out] out uint ReturnLength);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetNotificationResourceManagerAsync", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetNotificationResourceManagerAsync(
            [In] HANDLE ResourceManagerHandle,
            [Out] out TRANSACTION_NOTIFICATION TransactionNotification,
            [In] uint TransactionNotificationLength,
            [Out] out uint ReturnLength,
            [In] ref OVERLAPPED pOverlapped);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetTransactionId", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetTransactionId(
            [In] HANDLE TransactionHandle,
            [Out] out Guid TransactionId);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetTransactionInformation", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetTransactionInformation(
            [In] HANDLE TransactionHandle,
            [Out] out TRANSACTION_OUTCOME Outcome,
            [Out] out uint IsolationLevel,
            [Out] out uint IsolationFlags,
            [Out] out uint Timeout,
            [In] uint BufferLength,
            [Out] StringBuilder Description);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "GetTransactionManagerId", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetTransactionManagerId(
            [In] HANDLE TransactionManagerHandle,
            [Out] out Guid TransactionManagerId);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "OpenEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE OpenEnlistment(
            [In] EnlistmentAccessRights dwDesiredAccess,
            [In] HANDLE ResourceManagerHandle,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid EnlistmentId);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "OpenResourceManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE OpenResourceManager(
            [In] ResourceManagerAccessRights dwDesiredAccess,
            [In] HANDLE TmHandle,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid RmGuid);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "OpenTransaction", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE OpenTransaction(
            [In] TransactionAccessRights dwDesiredAccess,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid TransactionId);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "OpenTransactionManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE OpenTransactionManager(
            [In, MarshalAs(UnmanagedType.LPWStr)] string LogFileName,
            [In] TransactionManagerAccessRights dwDesiredAccess,
            [In] uint OpenOptions = 0);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "OpenTransactionManagerById", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HANDLE OpenTransactionManagerById(
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid TransactionManagerId,
            [In] TransactionManagerAccessRights dwDesiredAccess,
            [In] uint OpenOptions = 0);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "PrepareComplete", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool PrepareComplete(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "PrePrepareComplete", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool PrePrepareComplete(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "PrepareEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool PrepareEnlistment(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "PrePrepareEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool PrePrepareEnlistment(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "ReadOnlyEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ReadOnlyEnlistment(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RecoverEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RecoverEnlistment([In] HANDLE EnlistmentHandle, [In] IntPtr EnlistmentKey);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RecoverResourceManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RecoverResourceManager([In] HANDLE ResourceManagerHandle);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RecoverTransactionManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RecoverTransactionManager([In] HANDLE TransactionManagerHandle);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RenameTransactionManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RenameTransactionManager(
            [In, MarshalAs(UnmanagedType.LPWStr)] string LogFileName,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid ExistingTransactionManagerGuid);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RollbackComplete", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RollbackComplete(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RollbackEnlistment", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RollbackEnlistment(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RollbackTransaction", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RollbackTransaction([In] HANDLE TransactionHandle);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RollbackTransactionAsync", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RollbackTransactionAsync([In] HANDLE TransactionHandle);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "RollforwardTransactionManager", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RollforwardTransactionManager(
            [In] HANDLE TransactionManagerHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "SetEnlistmentRecoveryInformation", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetEnlistmentRecoveryInformation(
            [In] HANDLE EnlistmentHandle,
            [In] uint BufferSize,
            [In] IntPtr Buffer);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "SetResourceManagerCompletionPort", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetResourceManagerCompletionPort(
            [In] HANDLE ResourceManagerHandle,
            [In] HANDLE IoCompletionPortHandle,
            [In] UIntPtr CompletionKey);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "SetTransactionInformation", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetTransactionInformation(
            [In] HANDLE TransactionHandle,
            [In] uint IsolationLevel,
            [In] uint IsolationFlags,
            [In] uint Timeout,
            [In, MarshalAs(UnmanagedType.LPWStr)] string Description);

        [DllImport(Constants.ModuleNames.KTMW32, EntryPoint = "SinglePhaseReject", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SinglePhaseReject(
            [In] HANDLE EnlistmentHandle,
            [In] ref LARGE_INTEGER TmVirtualClock);
    }
}